﻿@model IEnumerable<DAMS.Models.Tag>

@{
    ViewData["Title"] = "标签管理";
}
<div class="row">
    <div class="col">
        <div class="btn-group btn-group-toggle">
            <button id="btn_del" type="button" class="btn btn-secondary ">删除</button>
            <button id="btn_edit" type="button" class="btn btn-secondary ">编辑</button>
            <a asp-action="Create" class="btn btn-secondary ">添加</a>
        </div>
    </div>
    <div class="col-4 input-group mb-3">
        <input type="text" class="form-control" placeholder="查找标签" id="txt_keyword" name="txt_keyword"  aria-label="Recipient's username" aria-describedby="btn_find">
        <div class="input-group-append">
            <button class="btn btn-outline-secondary" type="button" id="btn_find">搜索</button>
        </div>
    </div>
</div>

<div class="row">
    @foreach (var item in Model)
    {
        <button type="button" class="btn btn-info btn-sm m-4" dataid="@item.ID">@item.TagName</button>
    }
</div>
<div class="fixed-bottom text-center pb-5">
    <div class="btn-group" role="group" aria-label="Basic example" id="pager">
    </div>
</div>

@section Scripts {
    <script src="~/lib/layer/layer.js"></script>
    <script>
        var damsPager = {
            options: {
                dataCount: 0,
                pageSize: 3,
                pageIndex: 1,
                container: null
            },
            Create: function () {
                var pageNum = Math.ceil(this.options.dataCount / this.options.pageSize);
                var queryKeyword = this.options.keyword.length > 0 ? "&keyword=" + this.options.keyword : "";
                if (this.options.pageIndex > 1)
                    this.options.container.append('<a href="?pageIndex=' + (this.options.pageIndex - 1) + queryKeyword + '" class="btn btn-secondary" >上一页</a>')
                else
                    this.options.container.append('<a class="btn btn-secondary disabled" >上一页</a>');
                var startNum = 1, endNum = pageNum;

                if (pageNum > 5) {
                    startNum = this.options.pageIndex - 2;
                    endNum = this.options.pageIndex + 2;
                    if (this.options.pageIndex > 2) {

                        if (endNum > pageNum) {
                            endNum = pageNum;
                            startNum = endNum - 4;
                        }
                    } else {
                        if (startNum < 1) {
                            startNum = 1;
                            endNum = 5;
                        }
                    }

                };
                for (var i = startNum; i <= endNum; i++) {
                    if (i == this.options.pageIndex) {
                        this.options.container.append('<a class="btn btn-info" >' + i + '</a>');
                        continue;
                    }
                    this.options.container.append('<a href="?pageIndex=' + i + queryKeyword + '" class="btn btn-secondary" >' + i + '</a>')
                }
                if (this.options.pageIndex >= pageNum)
                    this.options.container.append('<a class="btn btn-secondary disabled" >下一页</a>');
                else
                    this.options.container.append('<a href="?pageIndex=' + (this.options.pageIndex + 1) + queryKeyword + '" class="btn btn-secondary" >下一页</a>');
            }
        }
        $(document).ready(function () {
            damsPager.options= {
                dataCount:@ViewData["TagCount"],
                pageIndex:@ViewData["PageIndex"],
                pageSize:@ViewData["PageSize"],
                keyword:'@ViewData["keyWord"]',
                container:$("#pager")
            };
            damsPager.Create();
            $("#txt_keyword").val(damsPager.options.keyword).keydown(function (event) {
                if (event.which == 13)
                    $("#btn_find").click();
            });
            var selectedid=0;
            $("button[dataid]").click(function () {
                var currid = $(this).attr('dataid');
                console.log("selectedid=" + selectedid)
                if (selectedid == currid) {
                    $(this).removeClass('btn-warning').addClass('btn-info');
                    selectedid = 0;
                    return;
                } else {
                    $("button[dataid='" + selectedid + "']").removeClass('btn-warning').addClass('btn-info');
                    $(this).removeClass('btn-info').addClass('btn-warning');
                }
                selectedid = currid;
            });
            $("#btn_del").click(function () {
                if (selectedid != 0) {
                    if (confirm("确定删除该标签码？")) {
                        window.location.href = "@Url.Action("Delete")" + "?id=" + selectedid + "&tagname=" + $("button[dataid='" + selectedid + "']").text();
                    }
                } else {
                    alert("未选择标签");
                }
            });
            $("#btn_edit").click(function () {
                if (selectedid != 0) {
                    window.location.href = "@Url.Action("Edit")" +  "?id=" + selectedid + "&tagname=" + $("button[dataid='" + selectedid + "']").text();
                } else {
                    alert("未选择标签");
                }
            });
            $("#btn_find").click(function () {
                document.location.href = "?keyword=" + $("#txt_keyword").val();
            })
        })
    </script>
}